package cn.lovecyy.mockjson.core.gen;

import cn.hutool.core.util.RandomUtil;
import cn.lovecyy.mockjson.core.resp.AreaInfo;
import cn.lovecyy.mockjson.core.resp.AreaResp;
import com.github.javafaker.Faker;
import com.github.javafaker.Job;
import com.github.javafaker.PhoneNumber;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

/**
 * FakerDataGen
 *
 * @author Yakir
 */
public class FakerDataGen implements IDataGen {

	private static Faker faker = new Faker(Locale.CHINA);

	private static Faker enFaker = new Faker();

	@Override
	public String chineseName() {
		return faker.name().fullName();
	}

	@Override
	public Map<String, Object> personInfo() {
		return randomPersonInfo();
	}

	public static Map<String, Object> randomPersonInfo() {
		Faker faker = new Faker(new Locale("zh-CN"));

		// 1. 随机选一个区域
		AreaInfo area = AreaResp.randomArea();

		// 2. 姓名
		String name = faker.name().fullName();

		// 3. 出生日期
		String birth = faker.date().birthday(18, 60).toInstant().atZone(java.time.ZoneId.systemDefault()).toLocalDate()
				.format(java.time.format.DateTimeFormatter.ofPattern("yyyyMMdd"));

		// 4. 顺序码
		String seq = String.format("%03d", RandomUtil.randomInt(0, 1000));

		// 5. 身份证号
		String id17 = area.getCountyCode() + birth + seq;
		char check = getCheckCode(id17);
		String idCard = id17 + check;
		// 性别
		char genderCode = idCard.charAt(16);
		String gender = (genderCode - '0') % 2 == 0 ? "女" : "男";

		// 生日
		String birthStr = idCard.substring(6, 14);
		String birthday = birthStr.substring(0, 4) + "-" + birthStr.substring(4, 6) + "-" + birthStr.substring(6, 8);
		String street = faker.address().streetName(); // 真实街道名
		String building = faker.address().buildingNumber(); // 门牌号
		String fullAddress = area.province + area.city + area.county + street + building + "号";
		// String zipCode = area.getZipCode(); // 新增邮编
		// 6. 返回配套信息
		Map<String, Object> map = new HashMap<>();
		map.put("name", name);
		map.put("idCard", idCard);
		map.put("gender", gender);
		map.put("birthday", birthday);
		map.put("provinceCode", area.provinceCode);
		map.put("cityCode", area.cityCode);
		map.put("countyCode", area.countyCode);
		map.put("address", fullAddress);
		map.put("province", area.province);
		map.put("city", area.city);
		map.put("county", area.county);
		map.put("street", street);
		map.put("building", building);
		// map.put("zipcode", zipCode); // 新增邮编
		return map;
	}

	// 校验码算法同前
	public static char getCheckCode(String id17) {
		int[] weight = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
		char[] checkCode = { '1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2' };
		int sum = 0;
		for (int i = 0; i < 17; i++) {
			sum += (id17.charAt(i) - '0') * weight[i];
		}
		return checkCode[sum % 11];
	}

	public static String randomCnapsCode(String bankName) {
		// 常见银行联行号前缀（前3~4位，实际可查表，这里简化）
		Map<String, String> cnapsPrefix = new HashMap<>();
		cnapsPrefix.put("中国工商银行", "102");
		cnapsPrefix.put("中国建设银行", "105");
		cnapsPrefix.put("中国农业银行", "103");
		cnapsPrefix.put("中国银行", "104");
		cnapsPrefix.put("招商银行", "308");
		cnapsPrefix.put("交通银行", "301");
		String prefix = cnapsPrefix.getOrDefault(bankName, "999");
		// 补齐12位
		StringBuilder sb = new StringBuilder(prefix);
		while (sb.length() < 12) {
			sb.append(RandomUtil.randomInt(0, 10));
		}
		return sb.toString();
	}

	@Override
	public String email() {
		return enFaker.internet().emailAddress();
	}

	@Override
	public String zipCode() {
		return faker.address().zipCode();
	}

	@Override
	public String address() {
		return faker.address().fullAddress();
	}

	public static void main(String[] args) {
		PhoneNumber phoneNumber = faker.phoneNumber();

		for (int i = 0; i < 1; i++) {
			System.out.println(randomPersonInfo());
			System.out.println(BankCardMockUtil.randomBankCardInfo());
			System.out.println(MockRandomUtils.randomPhone());
			System.out.println(phoneNumber.cellPhone());
			System.out.println(faker.job());
			System.out.println(enFaker.internet().emailAddress());
		}
	}

}
